/// f[i][j][d][m] : a string of length i, and j times to change the character, current direction
// is d, the sign of value is m, this stauts may not error, but it's too hard for write 
// dp[d][i][pos][cnt] : current direction is d, and in the i-th character, pos is the current
//position in x-axis, cnt the same meaning as n.
///
///
#include <stdio.h>
#include <string.h>

#define MAXL 100
#define MAXN 50

int dp[2][MAXL+5][MAXL*2+5][MAXN];
char cmd[MAXL + 1];

inline int max(int x, int y)
{
	return x > y ? x : y;
}
inline int abs(int x)
{
	return x > 0 ? x : -x;
}

int dfs(int d, int i, int pos, int cnt)
{
	if (cnt < 0)
		return 0;
	if (!cmd[i])
		return cnt > 0 ? 0: abs(pos);
	int &ans = dp[d][i][pos+100][cnt];
	if (ans != -1)
		return ans;
	int mv = d ? 1 : -1;
	return ans = max(dfs(d, i+1, pos+mv, cnt-(cmd[i]!='F')), dfs(1-d, i+1, pos, cnt-(cmd[i]!='T')) );
}

int main()
{
	int n;
	scanf("%s", cmd);
	scanf("%d", &n);
	
	int ans = 0;
	memset(dp, -1, sizeof(dp));

	for(; n >= 0 ; n -= 2)
	{
		ans = max(ans, dfs(1, 0, 0, n));
	}
	printf("%d\n", ans);
	return 0;
}